package com.android.advertising;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

public class MainService extends Service {

	private static MainService instance;
	NotificationCompat.Builder mBuilder;
	// Sets an ID for the notification
	int mNotificationId = 123;
	// Gets an instance of the NotificationManager service
	NotificationManager mNotifyMgr;

	public static MainService getInstance() {
		return instance;
	}

	public static boolean isReady() {
		if (instance != null)
			return true;
		else
			return false;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		mBuilder = new NotificationCompat.Builder(this);
		mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	long[] vibrate = { 100, 100, 200, 300 };

	public void addNotification(String contentTitle, String contentText, long id) {
		mBuilder.setSmallIcon(R.drawable.ic_launcher)
				.setContentTitle(contentTitle).setContentText(contentText)
				.setVibrate(vibrate);
		Intent intent = new Intent(this, ArticleDetailActivity.class);
		intent.putExtra(Constants.ARTICLES_ID, id);
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);	
		mBuilder.setContentIntent(pIntent);
		notifyWrapper(mBuilder.build());
	}

	private void notifyWrapper(Notification notify) {
		// Builds the notification and issues it.
		notify.defaults |= Notification.DEFAULT_SOUND;
		notify.flags |= Notification.FLAG_SHOW_LIGHTS;
		notify.ledARGB = 0xff00ff00;
		notify.ledOnMS = 3000;
		notify.ledOffMS = 1000;
		mNotifyMgr.notify(mNotificationId, notify);
	}
	
	public void removeNotification(){
		mNotifyMgr.cancelAll();
	}
}
